/*************************************************************************
	> File Name: P1387.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-04-23 15:35:04
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 100
int g[MAX_N + 5][MAX_N + 5];

int binary_search(int i, int j, int n, int m) {
    int head = 0, tail = min(n, m), mid;
    while (head < tail) {
        mid = (head + tail + 1) >> 1;
        int k = i + mid - 1, l = j + mid - 1;
        if (k > n || l > m || g[k][l] - g[i - 1][l] - g[k][j - 1] + g[i - 1][j - 1] < mid * mid) tail = mid - 1;
        else head = mid;
    }
    return head;
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
            g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int len = binary_search(i, j, n, m);
            int k = i + len - 1, l = j + len - 1;
            int val = g[k][l] - g[i - 1][l] - g[k][j - 1] + g[i - 1][j - 1];
            if (val == len * len) {
                ans = max(ans, len);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
